function [ results ] = checkRects( results, gen_flag, overlap_flag )
if nargin == 2
    overlap_flag = true;
end
load_settings;

disp('CheckRects: Cleaning up positive examples');
for ix = 1:length(results)
    fn = strcat(image_root, sprintf('%08d', results(ix).cam_id), '/', results(ix).filename);
    img = imread(fn);
    [iheight iwidth irgb] = size(img);
    max_ind = size(results(ix).ppl_rects, 1);
    jx = 1;
    while jx <= max_ind;
        % Future: this should be vectorized
        cur_rect = results(ix).ppl_rects(jx,:);
        
        % Negative width checks
        if cur_rect(3) < 0;
            cur_rect(1)  = cur_rect(1) + cur_rect(3);
            cur_rect(3) = cur_rect(3) * -1;
        end
        if cur_rect(4) < 0;
            cur_rect(2)  = cur_rect(2) + cur_rect(4);
            cur_rect(4) = cur_rect(4) * -1;
        end
        
        omit = 0; % false
        % Omit if width is less than the bin_size. Otherwise can't compute HoG features.
        if cur_rect(3) < binsize;
            omit = 1;
        end
        if cur_rect(4) < binsize;
            omit = 1;
        end
        % Inflate rectangles
        cur_rect = cur_rect + [-inflate_margin, -inflate_margin, 2*inflate_margin, 2*inflate_margin];
        
        
        x = cur_rect(1);
        y = cur_rect(2);
        xx = cur_rect(1)+cur_rect(3);
        yy = cur_rect(2)+cur_rect(4);
        
        %check if rectangle is completely outside of image, if only
        %partially then crop to edges
        if x > iwidth; % x is outside width
            omit = 1;
        end
        if y > iheight;
            omit = 1;
        end
        if x <= 0; % crop to left edge
            cur_rect(1) = 1;
        end
        if y <= 0; % crop to top edge
            cur_rect(2) = 1;
        end
        if xx > iwidth; % crop to right
            cur_rect(3) = iwidth - x;
        end
        if yy > iheight; % crop to bottom
            cur_rect(4) = iheight - y;
        end
        
        
        if omit;
            results(ix).ppl_rects(jx,:) = [];
            max_ind = max_ind-1;
        else
            results(ix).ppl_rects(jx,:) = cur_rect;
            jx = jx+1;
        end
    end
end

%% Generate random bg rects, if gen_flag is specified
if gen_flag
    disp('CheckRects: Generating random background examples');
    for ix = 1:length(results)
        fn = strcat(image_root, sprintf('%08d', results(ix).cam_id), '/', results(ix).filename);
        img = imread(fn);
        [iheight iwidth irgb] = size(img);
        for jx = 1:size(results(ix).ppl_rects, 1)
            x = randi([1, iwidth - results(ix).ppl_rects(jx,3)]); % ensure it doesn't go off the edge so I don't have to do lots of checks
            y = randi([1, iheight- results(ix).ppl_rects(jx,4)]);
            xx= results(ix).ppl_rects(jx,3);
            yy= results(ix).ppl_rects(jx,4);
            results(ix).bg_rects(jx,:) = [x, y, xx, yy];
        end
    end
end


%% Omit negative rectangles if they overlap at all with any positive example in the same set
% NOTE: For balanced sets, this can make them slightly unbalanced?
% Question: In that case, do we want to generate a new random example? Do
% we care that it would be slightly unbalanced?
if overlap_flag
disp('CheckRects: Removing overlapping rectangles');
for ix = 1:length(results)
    for jx = 1:size(results(ix).ppl_rects,1)
        bi = 1;
        cur_ppl = results(ix).ppl_rects(jx,:);
        while bi <= size(results(ix).bg_rects,1)
            cur_bg = results(ix).bg_rects(bi, :);
            overlap = rectint(cur_ppl, cur_bg);
            if overlap > 0
                results(ix).bg_rects(bi, :) = [];
            else
                bi = bi+1;
            end
        end
    end
end
end

%% If after all checks, no rectangles are left, remove entry entirely
disp('checkRects: Final check');
ll = fliplr(1:length(results));
for ix = ll
    if isempty(results(ix).ppl_rects)
        results(ix) = [];
    end
end
end